﻿using BingVis.GoogleTransit;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BingVis.Common;
using System.Collections.Generic;
using System.Threading;

namespace BingVis.GoogleTransit.Test
{
    
    
    /// <summary>
    ///This is a test class for TransitTripLoaderTest and is intended
    ///to contain all TransitTripLoaderTest Unit Tests
    ///</summary>
    [TestClass()]
    public class TransitTripLoaderTest
    {
        string[] testTripContents = 
        {
            "route_id,service_id,trip_id,trip_headsign,direction_id,block_id,shape_id",
            "741_-1056,BUSS39-hbs39011-Weekday-03,11278542-BUSS39-hbs39011-Weekday-03,Logan Airport,,1468010,SL1OUT",
            "741_-1056,BUSS39-hbs39011-Weekday-03,11278543-BUSS39-hbs39011-Weekday-03,Logan Airport,,1467884,SL1OUT",
            "741_-1056,BUSS39-hbs39011-Weekday-03,11278544-BUSS39-hbs39011-Weekday-03,Logan Airport,,1467884,SL1OUT",
            "741_-1056,BUSS39-hbs39011-Weekday-03,11278545-BUSS39-hbs39011-Weekday-03,Logan Airport,,1467886,SL1OUT",
            "741_-1056,BUSS39-hbs39011-Weekday-03,11278546-BUSS39-hbs39011-Weekday-03,Logan Airport,,1468008,SL1OUT",
            "741_-1056,BUSS39-hbs39011-Weekday-03,11278547-BUSS39-hbs39011-Weekday-03,Logan Airport,,1468010,SL1OUT",
            "741_-1056,BUSS39-hbs39011-Weekday-03,11278548-BUSS39-hbs39011-Weekday-03,Logan Airport,,1467884,SL1OUT",
            "741_-1056,BUSS39-hbs39011-Weekday-03,11278549-BUSS39-hbs39011-Weekday-03,Logan Airport,,1467886,SL1OUT",
            "741_-1056,BUSS39-hbs39011-Weekday-03,11278550-BUSS39-hbs39011-Weekday-03,Logan Airport,,1468008,SL1OUT",
            "741_-1056,BUSS39-hbs39011-Weekday-03,11278913-BUSS39-hbs39011-Weekday-03,Logan Airport,,1467884,SL1OUT",
        };

        /// <summary>
        ///A test for ProcessItem
        ///</summary>
        [TestMethod()]
        public void Should_Process_Items_Into_Trips_With_Proper_Values()
        {
            IGTFSFile file = new GTFSFile("trips.txt", testTripContents);
            var stopTimes = new List<ITransitStopTime>();
            TransitTripLoader loader = new TransitTripLoader(file, stopTimes);
            int count = 0;
            bool done = false;

            loader.ItemLoaded += (x, y) => count++;
            loader.DoneProcessing += (x, y) => done = true;

            loader.ProcessFile();
            while (!done)
            { Thread.Sleep(150); }

            var target = loader.Items[0];

            // Verify values.
            //"route_id,service_id,trip_id,trip_headsign,direction_id,block_id,shape_id",
            //"741_-1056,BUSS39-hbs39011-Weekday-03,11278542-BUSS39-hbs39011-Weekday-03,Logan Airport,,1468010,SL1OUT",

            Assert.AreEqual<string>("BUSS39-hbs39011-Weekday-03", target.ServiceId);
            Assert.AreEqual<string>("741_-1056", target.RouteId);
            Assert.AreEqual<string>("11278542-BUSS39-hbs39011-Weekday-03", target.Id);
            Assert.AreEqual<string>("Logan Airport", target.Headsign);
            Assert.AreEqual<bool>(false, target.DirectionId);  // ? Empty string value parsed to bool ?
            Assert.AreEqual<string>("1468010", target.BlockId);
            Assert.AreEqual<string>("SL1OUT", target.ShapeId);
        }

        /// <summary>
        ///A test for ProcessItem
        ///</summary>
        [TestMethod()]
        public void Should_Process_10_Trips_From_TestContents()
        {
            IGTFSFile file = new GTFSFile("trips.txt", testTripContents);
            var stopTimes = new List<ITransitStopTime>();
            TransitTripLoader target = new TransitTripLoader(file, stopTimes);
            int count = 0;
            bool done = false;

            target.ItemLoaded += (x, y) => count++;
            target.DoneProcessing += (x, y) => done = true;

            target.ProcessFile();
            while (!done)
            { Thread.Sleep(150); }

            Assert.AreEqual<int>(10, count);
            Assert.AreEqual<int>(count, target.Items.Count);
        }

        /// <summary>
        ///A test for TransitTripLoader Constructor
        ///</summary>
        [TestMethod()]
        public void Should_Initialize_Proper_Values()
        {
            IGTFSFile file = new GTFSFile("trips.txt", testTripContents);
            var stopTimes = new List<ITransitStopTime>();
            TransitTripLoader target = new TransitTripLoader(file, stopTimes);

            Assert.IsNotNull(target.Items);
            Assert.AreEqual<int>(0, target.Items.Count);
        }
    }
}
